/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * BaseRemindConfigService
 * 提醒配置表
 * 修改纪录
 * 2020-10-29 版本：1.0 zhanglongfei 创建。
 * 
 * @version 版本：1.0
 * @author 作者：zhanglongfei
 * 创建日期：2020-10-29
 */
package com.topscomm.base.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.topscomm.base.pojo.BaseRemindConfigEntity;
import com.topscomm.base.service.IBaseRemindConfigService;
import com.topscomm.base.service.impl.auto.BaseRemindConfigServiceAuto;
import com.topscomm.meet.pojo.MeetManageEntity;
import com.topscomm.meet.pojo.MeetPeroidTaskEntity;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.work.WorkSystemConst.WorkSourceTypeEnum;

@Service(value = "baseRemindConfigService")
@Scope(value = "singleton")
public class BaseRemindConfigService extends BaseRemindConfigServiceAuto implements IBaseRemindConfigService {

	/**
	 * @description:批量保存消息提醒配置
	 * @param sourceId
	 * @param sourceType
	 * @param list
	 * @author: zhanglongfei
	 * @date: 2020年10月29日下午3:05:37
	 * @modify:
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchSaveRemindConfigList(String sourceId, String sourceType, List<Map<String, Object>> list) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendLineSingle("sourcetype='{0}' and sourceid='{1}'", sourceType, sourceId);
		List<BaseRemindConfigEntity> oldList = queryByWhere(sql.toString());
		// 找出删除的行
		List<String> deleteList = new ArrayList<String>();
		for (BaseRemindConfigEntity old : oldList) {
			if (!list.stream().anyMatch(m -> m.get("id").toString().equals(String.valueOf(old.getId())))) {
				deleteList.add(String.valueOf(old.getId()));
			}
		}
		// 新增的新增，删除的删除，修改的修改
		for (Map<String, Object> item : list) {
			BaseRemindConfigEntity newEntity = new BaseRemindConfigEntity();
			newEntity.convertFromMap(item);
			newEntity.setSourceid(sourceId);
			newEntity.setSourcetype(sourceType);
			// 这个类型的不提醒，插入仅为了之后修改显示用
			if (newEntity.getState() == 0) {
				if (WorkSourceTypeEnum.WorkTask.equals(sourceType) || MeetManageEntity.tableName.equals(sourceType)
						|| MeetPeroidTaskEntity.tableName.equals(sourceType))
					newEntity.setState(-1);
				else {
					newEntity.setState(0);
				}
			}
			if (newEntity.getId() > 0) {
				updateEntity(newEntity);
			} else {
				insert(newEntity);
			}
		}
		if (deleteList.size() > 0)
			deleteByIds(String.join(",", deleteList));
	}
}
